package qlsl.androiddesign.activity.commonactivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import qlsl.androiddesign.activity.baseactivity.BaseActivity;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.entity.commonentity.City;
import qlsl.androiddesign.util.commonutil.Util;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * Created by Administrator on 2016/10/10 0010.
 */
public class SelectCityActivity extends BaseActivity implements
		AdapterView.OnItemClickListener,
		ExpandableListView.OnChildClickListener {

	private String key;// 查询key

	ImageView mActivityTopLeft;
	TextView mActivityTopCenter;
	ImageView mActivityTopRight;
	TextView mActivityTopRightText;
	EditText searchEditText;
	TextView currCityTitle;
	TextView mCurrCitytView;
	ExpandableListView mExpandableListView;
	LinearLayout mSelectCityLayout;
	ListView mSearchListView;

	private MyExpandAdapter mAdapter;
	private List<City> mCitiesList;

	private List<City> searchCityList = new ArrayList<City>();
	private City mCurrentCity;
	private boolean isFirstSelectCity = false;
	private Bundle bundle;
	private Util util = Util.getInstance();
	// private LocationClient mLocationClient = null;

	private BaseAdapter searchAdapter = new BaseAdapter() {
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			City city = searchCityList.get(position);
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(SelectCityActivity.this)
						.inflate(R.layout.item_city, parent, false);
				viewHolder = new ViewHolder();
				viewHolder.pro = (TextView) convertView
						.findViewById(R.id.item_city);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.pro.setText(city.getArea_name());
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return searchCityList.get(position);
		}

		@Override
		public int getCount() {
			return searchCityList.size();
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_city);
		findView();
		initView();

	};

	TextView tv_title;
	View btn_back, btn_right;

	public void findView() {
		searchEditText = (EditText) findViewById(R.id.select_city_search);
		currCityTitle = (TextView) findViewById(R.id.position_city_title);
		mCurrCitytView = (TextView) findViewById(R.id.select_city_curr);
		mExpandableListView = (ExpandableListView) findViewById(R.id.select_city_listview);
		mSelectCityLayout = (LinearLayout) findViewById(R.id.select_city_layout);
		mSearchListView = (ListView) findViewById(R.id.search_city_listview);
		tv_title = (TextView) findViewById(R.id.tv_title);
		btn_back = (View) findViewById(R.id.btn_back);
		btn_right = findViewById(R.id.btn_right);
		tv_title.setText("城市");
		btn_right.setVisibility(View.INVISIBLE);
		currCityTitle.setText("定位城市");
		mCurrCitytView.setText("" + Constant.city);

		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void initView() {
		bundle = getIntent().getExtras();
		mCurrentCity = new City();
		if (bundle != null)
			isFirstSelectCity = bundle.getBoolean(Constant.SGETUSERLOCATION);

		// this.title.setText(getResources().getString(R.string.select_city));//
		// 初始化title
		if (isFirstSelectCity) {
			// btnBack.setVisibility(View.INVISIBLE);
		} else {
			// this.btnBack.setOnClickListener(listener);
		}
		loadArea();
		this.searchEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				key = s.toString().trim();
				if (key.length() > 0) {
					toggle(true);
					searchArea(key);
				} else {
					toggle(false);
				}
			}
		});
		this.mSearchListView.setAdapter(searchAdapter);
		this.mSearchListView.setOnItemClickListener(this);
		mExpandableListView.setOnChildClickListener(this);

	}

	private void toggle(boolean showSearchListView) {
		if (showSearchListView) {
			mSearchListView.setVisibility(View.VISIBLE);
			mSelectCityLayout.setVisibility(View.INVISIBLE);
			// 这样才能实现第一次的时候隐藏这些view
		} else {
			mSearchListView.setVisibility(View.INVISIBLE);
			mSelectCityLayout.setVisibility(View.VISIBLE);
		}
	}

	private void searchArea(String cityName) {
		if (searchCityList.size() > 0)
			searchCityList.clear();
		for (City city : mCitiesList) {
			if (city.getArea_name().equals(cityName)
					|| city.getArea_name().contains(cityName)) {
				searchCityList.add(city);
			} else {
				ArrayList<City> sCities = city.getSubCitys();
				if (sCities != null && sCities.size() > 0) {
					for (City sCity : sCities) {
						if (sCity.getArea_name().equals(cityName)
								|| sCity.getArea_name().contains(cityName))
							searchCityList.add(sCity);
					}
				}
			}

		}
		searchAdapter.notifyDataSetChanged();

	}

	private void loadArea() {

		mCitiesList = Util.getInstance().loadArea(this);
		// 省
		setExpandAdapter();
	}

	private void setExpandAdapter() {
		this.mAdapter = new MyExpandAdapter();
		this.mExpandableListView.setAdapter(this.mAdapter);
		mExpandableListView.setGroupIndicator(null);
		mExpandableListView.setFooterDividersEnabled(false);
		mExpandableListView
				.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
					@Override
					public boolean onGroupClick(ExpandableListView parent,
							View v, int groupPosition, long id) {
						final City city = mCitiesList.get(groupPosition);
						if (city.getSubCitys() == null
								|| city.getSubCitys().size() < 1) {
							mCurrentCity = city;
							finish(mCurrentCity, null);
							return true;
						}
						return false;
					}
				});
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		City city = mCitiesList.get(groupPosition);
		finish(mCitiesList.get(groupPosition).getSubCitys().get(childPosition),
				city);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		finish(searchCityList.get(position), null);
	}

	private class MyExpandAdapter extends BaseExpandableListAdapter {
		@Override
		public int getGroupCount() {
			return mCitiesList.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mCitiesList.get(groupPosition).getSubCitys().size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mCitiesList.get(groupPosition);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return mCitiesList.get(groupPosition).getSubCitys()
					.get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return mCitiesList.get(groupPosition).getArea_id();
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return mCitiesList.get(groupPosition).getSubCitys()
					.get(childPosition).getArea_id();
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			City city = mCitiesList.get(groupPosition);
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(SelectCityActivity.this)
						.inflate(R.layout.item_province, parent, false);
				viewHolder = new ViewHolder();
				viewHolder.pro = (TextView) convertView
						.findViewById(R.id.item_pro);
				viewHolder.icon = (ImageView) convertView
						.findViewById(R.id.item_pro_icon);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			if (city.getSubCitys().size() <= 0) {
				viewHolder.icon.setImageDrawable(null);
			} else {
				if (isExpanded) {
					viewHolder.icon
							.setImageResource(R.drawable.list_tittle_arrow2);
				} else {
					viewHolder.icon
							.setImageResource(R.drawable.list_tittle_arrow1);
				}
			}
			viewHolder.pro.setText(city.getArea_name());
			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			City city = mCitiesList.get(groupPosition).getSubCitys()
					.get(childPosition);
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(SelectCityActivity.this)
						.inflate(R.layout.item_city, parent, false);
				viewHolder = new ViewHolder();
				viewHolder.pro = (TextView) convertView
						.findViewById(R.id.item_city);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.pro.setText(city.getArea_name());
			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

	}

	public static class ViewHolder {
		public TextView pro;
		public ImageView icon;
	}

	// 统一退出函数
	private void finish(City city, City city2) {
		// if (!Constant.isGetLocation) {// 定位失败
		// Util.getInstance().saveCity(SelectCityActivity.this, city);
		// util.go2Activity(SelectCityActivity.this, CarSharingActivity.class);
		// } else {
		Intent intent = getIntent();
		if (city2 != null) {
			intent.putExtra("city2", city2);
		}
		intent.putExtra("city", city);
		setResult(Constant.SELECT_CITY_RESULT_CODE, intent);
		// }
		super.finish();
	}

	@Override
	public FunctionView<?> getFunctionView() {
		// TODO Auto-generated method stub
		return null;
	}

}
